package com.esd.db.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.esd.db.dao.VtdNoticeMapper;
import com.esd.db.model.VtdNotice;
import com.esd.db.service.VtdNoticeService;

@Service("VtdNoticeService")
public class VtdNoticeServiceImpl implements VtdNoticeService {
	@Autowired
	VtdNoticeMapper noticeMapper;

	@Override
	public VtdNotice selectById(int id) {
		return noticeMapper.selectById(id);
	}

	@Override
	public int addNoticeAndSend(VtdNotice notice) {
		int num = noticeMapper.insertVtdNotice(notice);
		if(num == 1){
			noticeMapper.updateAllUserNoticeFlag(1);  //更新user表通知公告字段 为1 开启 
		}
		return num;
	}

	@Override
	public List<VtdNotice> getAllNoticeOrderByUpdateTimeDesc(String condition, int page, int row) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (condition.trim().length() > 0) {
			map.put("condition", condition);
		} else {
			map.put("condition", null);
		}
		if (page == 0) {
			map.put("begin", null);
			map.put("end", null);
		} else {
			map.put("begin", (page - 1) * row);
			map.put("end", row);
		}
		return noticeMapper.selectAllOrderByUpdateTimeDesc(map);
	}

	@Override
	public int getAllNoticeOrderByUpdateTimeDescCount(String condition) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (condition.trim().length() > 0) {
			map.put("condition", condition);
		} else {
			map.put("condition", null);
		}
		return noticeMapper.selectAllNoticeOrderByUpdateTimeDescCount(map);
	}

	@Override
	public int updateNoticeById(VtdNotice notice) {
		return noticeMapper.updateNoticeBy(notice);
	}

	@Override
	public int noticeSend() {
		int num = noticeMapper.updateAllUserNoticeFlag(1);  //更新user表通知公告字段 为1 开启 
		return num;
	}

	@Override
	public VtdNotice getOneNoticeOrderByUpdateTimeDesc() {
		return noticeMapper.getOneNoticeOrderByUpdateTimeDesc();
	}

	@Override
	public int neverReceiveByUserId(int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		map.put("flag", 0);
		return noticeMapper.updateUserNoticeFlagByUserId(map);
	}
	
	
}
